package com.bw.goods.mvvm.viewmodel;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.goods.mvvm.adapter.CateGoryAdapter;
import com.bw.goods.mvvm.adapter.CategoryrightAdapter;
import com.bw.goods.mvvm.model.CateGoryModel;
import com.bw.goods.mvvm.model.entity.CateGoryliftEntity;
import com.bw.goods.mvvm.model.entity.CategoryrightEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/16 0016 上午 10:14
 *
 */
class CateGoryViewModel extends BaseViewModel<CateGoryModel> {
    public BaseQuickAdapter rightadapter = new CategoryrightAdapter();
    public BaseQuickAdapter adapter = new CateGoryAdapter();
    List<CateGoryliftEntity.DataBean> data;
    @Override
    public void onCreate() {
        super.onCreate();
        request();
//        request2();
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick( BaseQuickAdapter<?, ?> adapter,  View view, int position) {
                requestrightcategory(data.get(position).getId());
            }
        });
    }
    private void request(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("parentId",0);
        m.requestCateGoryleft(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }
    private void requestrightcategory(int id){
        HashMap<String,Object> map = new HashMap<>();
        map.put("parentId",id);
        m.requestCateGory(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe( Disposable d) {

                    }

                    @Override
                    public void onNext(  BaseEntity entity) {
                        CategoryrightEntity categoryrightEntity = (CategoryrightEntity) entity;

                        List<CategoryrightEntity.DataBean> data = categoryrightEntity.getData();
                        LogUtils.e("dddddddddddddddddddddd"+data);
                        rightadapter.setNewInstance(data);

                    }

                    @Override
                    public void onError( Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    @Override
    protected void result(BaseEntity entity) {
        CateGoryliftEntity cateGoryliftEntity = (CateGoryliftEntity) entity;
        data = cateGoryliftEntity.getData();
        LogUtils.e("cccccccccccccccccc"+cateGoryliftEntity.getData());
        adapter.setNewInstance(cateGoryliftEntity.getData());

    }



}
